package com.syh.jn.scm.service.impl.downstream.base;

import cn.redscarf.commons.util.bean.ExtBeanUtils;
import cn.redscarf.commons.wrapper.Wrapper;
import com.alibaba.dubbo.config.annotation.Reference;
import com.syh.core.test.spring.SpringTestBase;
import com.syh.jn.scm.dto.downstream.base.logistics.DownLogisticsMto;
import com.syh.jn.scm.dto.downstream.base.logistics.DownLogisticsSto;
import com.syh.jn.scm.dto.downstream.base.logistics.DownLogisticsVo;
import com.syh.jn.scm.dto.downstream.base.logistics.logisticsAddress.DownLogisticsAddressMto;
import com.syh.jn.scm.dto.downstream.base.logistics.logisticsAddress.DownLogisticsAddressSto;
import com.syh.jn.scm.dto.downstream.base.logistics.logisticsAddress.DownLogisticsAddressVo;
import com.syh.jn.scm.dto.downstream.base.logistics.logisticsTruck.DownLogisticsTruckMto;
import com.syh.jn.scm.dto.downstream.base.logistics.logisticsTruck.DownLogisticsTruckSto;
import com.syh.jn.scm.dto.downstream.base.logistics.logisticsTruck.DownLogisticsTruckVo;
import com.syh.jn.scm.service.api.downstream.base.DownLogisticsService;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

/**
 * 客户物流测试
 */
public class DownLogisticsServiceImplTesst extends SpringTestBase {

  @Reference
  private DownLogisticsService downLogisticsServiceImpl;

  /**
   * 新增
   */
  @Test
  public void save() {
    Long id = this.createLogistic();
    System.out.println(id);
  }
  /**
   * 修改
   */
  @Test
  public void update() {
    Long id = this.createLogistic();
    DownLogisticsVo vo = downLogisticsServiceImpl.getById(id);
    System.out.println("更改前："+vo);
    DownLogisticsMto mto = new DownLogisticsMto();
    ExtBeanUtils.copyProperties(vo,mto);
    mto.setName("更改物流名称");
    mto.setTelephoneNum("更改电话");
    mto.setIsProvide(Short.valueOf("-1"));
    downLogisticsServiceImpl.update(mto);
    vo = downLogisticsServiceImpl.getById(id);
    System.out.println("更改后："+vo);
  }

  /**
   *添加服务地址 服务车型
   */
  @Test
  public void addAddress(){
    this.craeateAddress();
  }

  /**
   *修改服务地址 服务车型
   */
  @Test
  public void editAddress(){
    Long id = this.craeateAddress();
    List<DownLogisticsAddressVo> addressVoList = downLogisticsServiceImpl.getAddressListById(id);
    List<DownLogisticsAddressMto> addressMtoList = new ArrayList<>();
    addressVoList.forEach(addressVo -> {
      DownLogisticsAddressMto addressMto = new DownLogisticsAddressMto();
      ExtBeanUtils.copyProperties(addressVo,addressMto);
      addressMto.setMobile("更改联系电话");
      addressMtoList.add(addressMto);
    });
    downLogisticsServiceImpl.batchModifyAddress(addressMtoList);
    List<DownLogisticsTruckVo> truckVoList = downLogisticsServiceImpl.getTruckListById(id);
    List<DownLogisticsTruckMto> truckMtoList = new ArrayList<>();
    truckVoList.forEach(truckVo -> {
      DownLogisticsTruckMto truckMto = new DownLogisticsTruckMto();
      ExtBeanUtils.copyProperties(truckVo,truckMto);
      truckMto.setIsColdChain(Short.valueOf("-1"));
      truckMtoList.add(truckMto);
    });
    downLogisticsServiceImpl.batchModifyTruck(truckMtoList);

  }

  @Test
  public void getList(){
    List<DownLogisticsVo> downLogistics = downLogisticsServiceImpl.findList(Wrapper.build());
    System.out.println(downLogistics);
  }



  public Long createLogistic() {
    DownLogisticsSto sto = new DownLogisticsSto();
    sto.setBusinessEntityId(1000000L);
    sto.setName("测试下游物流");
    sto.setTelephoneNum("公司联系电话");
    sto.setIsProvide(Short.valueOf("1"));
    Long id = downLogisticsServiceImpl.save(sto);
    return id;
  }

  public Long craeateAddress() {
    Long id = this.createLogistic();
    DownLogisticsTruckSto  truckSto = new DownLogisticsTruckSto();
    truckSto.setLogisticsId(id);
    truckSto.setTruckType(Short.valueOf("1"));
    truckSto.setIsColdChain(Short.valueOf("1"));
    List<DownLogisticsTruckSto> truckStoList = new ArrayList<>();
    truckStoList.add(truckSto);
    downLogisticsServiceImpl.batchSaveTruck(truckStoList);
    DownLogisticsAddressSto addressSto = new DownLogisticsAddressSto();
    addressSto.setLogisticsId(id);
    addressSto.setAddress("测试添加服务地址");
    addressSto.setArea("测试添加省市区");
    addressSto.setAreaCode("测试添加地区编号");
    addressSto.setContacts("测试添加联系人");
    addressSto.setMobile("测试添加联系电话");
    List<DownLogisticsAddressSto> addressStoList = new ArrayList<>();
    addressStoList.add(addressSto);
    downLogisticsServiceImpl.batchSaveAddress(addressStoList);
    return id;
  }
}
